package com.kaishengit.web;


import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.dao.OrderDao;
import com.kaishengit.dao.ProdDao;
import com.kaishengit.entity.Prod;
import com.kaishengit.entity.User;

public class MyOrderServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
		@Override
		protected void service(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			
			HttpSession session=request.getSession();
			User user=(User)session.getAttribute("user");
			
			OrderDao dao=new OrderDao();
			ProdDao prodDao=new ProdDao();
			
			List<Map<String,Object>> list=dao.findByUserId(user.getId());
			
			for(Map<String,Object> map:list){
				List<Prod> prodList=prodDao.findByOrderId((String)map.get("id"));
				map.put("names",listToString(prodList));
				
			}
			request.setAttribute("list",list);
			request.getRequestDispatcher("/WEB-INF/views/myorder.jsp").forward(request,response);
			
		}
	private String listToString(List<Prod> prodList){
		StringBuffer sb=new StringBuffer();
		for(Prod p:prodList){
			sb.append(p.getProdName()).append(",");
		}
		String str=sb.toString();
		str=str.substring(0,str.length());
		return str;
	}
	
}
